<?php

namespace app\admin\controller;

use app\admin\model\GoodsModel;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * 积分商城订单管理
 * @package app\admin\controller
 */
class OrdersController extends AdminBaseController
{



    public function index()
    {
        $map    = [];
        $data   = $this->request->param();

        $list = DB::name('goods_record')->where($map)->order("list_order asc")->paginate(10, false, ['query' => input()]);

        $goodsData = db('goods')->column(['id', 'title']);
        $usersData = db('users')->column(['id', 'user_nickname']);

        $list->each(function ($v) use($goodsData, $usersData){
            $gid = $v['goods_id'];
            $uid = $v['uid'];
            $v['goods_name'] = '';
            $v['user_nickname'] = '';
            if (isset($goodsData[$gid])) {
                $v['goods_name'] = $goodsData[$gid];
            }
            if (isset($usersData[$uid])) {
                $v['user_nickname'] = $usersData[$uid];
            }

            $v['addtime'] = date('Y-m-d H:i:s', $v['addtime']);

            return $v;
        });

        $page = $list->render();
        $this->assign("page", $page);
        $this->assign('list', $list);

        return $this->fetch();
    }


    public function add()
    {

        return $this->fetch();
    }


    public function addPost()
    {
        if ($this->request->isPost()) {
            $data           = $this->request->param();
            if ($data['multi_thumb'] ?? '') {
                $data['multi_thumb'] = implode(',', $data['multi_thumb']);
            }

            $validate         = $this->validate($data, 'Goods');
            if ($validate !== true) {
                $this->error($validate);
            }
            $data['addtime']=time();
            $result         = DB::name('goods')->insert($data);
            if (!$result) {
                $this->error($result);
            }
            $this->success("添加成功！", url("shop/goods"));
        }
    }

    public function edit()
    {
        $id             = $this->request->param('id');
        $result          = DB::name('goods')->where('id',$id)->find();
        $this->assign('result', $result);

        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $validate         = $this->validate($data, 'Goods');
            if ($validate !== true) {
                $this->error($validate);
            }
            $data['addtime']=time();
            $slidePostModel = DB::name('goods')->where('id',$data['id'])->update($data);

            if($slidePostModel){
                $this->success("保存成功！", url("shop/goods"));
            }
            $this->error('信息错误');
        }
    }


    public function delete()
    {
        if ($this->request->isPost()) {
            $id             = $this->request->param('id', 0, 'intval');
            if(DB::name('goods')->where('id',$id)->delete()){
                $this->success("删除成功！", url("shop/goods"));
            }

            $this->error('信息错误');
        }
    }


    //发货
    public function sendorder() {
        $map    = [];
        $data   = $this->request->param();
        if ($data['id'] ?? 0) {
            $res = db('goods_record')->where('id', $data['id'])->update(['is_send' => 1]);
            if ($res) {
                $this->success("操作成功！", url("orders/index"));
            }
        }

        $this->error('操作失败');

    }


    public function listOrder()
    {
        $lessionpageModel = new  GoodsModel();
        parent::listOrders($lessionpageModel);
        $this->success("排序更新成功！");
    }

}
